home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow_WinXP / VMR / VMRXcl / AllocPresenter.cpp next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  10.6 KB  |  372 lines

  1. //----------------------------------------------------------------------------
  2. //  File:   AllocPresenter.cpp
  3. //
  4. //  Desc:   DirectShow sample code
  5. //          Implementation of user-provided allocator-presenter for VMR
  6. //
  7. //  Copyright (c) 2000-2001 Microsoft Corporation. All rights reserved.
  8. //----------------------------------------------------------------------------
  9.  
  10. #include "project.h"
  11. #include <mmreg.h>
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include "resrc1.h"
  16. #include "D3DTextr.h"
  17. #include "utils.h"
  18.  
  19. #ifndef __INITDDSTRUCT_DEFINED
  20. #define __INITDDSTRUCT_DEFINED
  21. template <typename T>
  22. __inline void INITDDSTRUCT(T& dd)
  23. {
  24.     ZeroMemory(&dd, sizeof(dd));
  25.     dd.dwSize = sizeof(dd);
  26. }
  27. #endif
  28.  
  29.  
  30. //----------------------------------------------------------------------------
  31. // CreateDefaultAllocatorPresenter
  32. //
  33. // creates user-provides allocator presenter
  34. // 
  35. // Usually you have to actually override several functions of AllocatorPresenter;
  36. // For the rest, QI IVMRImagePresenter and call default functions
  37. //----------------------------------------------------------------------------
  38. HRESULT
  39. CMpegMovie::CreateDefaultAllocatorPresenter(
  40.     LPDIRECTDRAW7 lpDD,
  41.     LPDIRECTDRAWSURFACE7 lpPS
  42.     )
  43. {
  44.     HRESULT hr = S_OK;
  45.     IVMRImagePresenterExclModeConfig* lpConfig = NULL;
  46.  
  47.     __try {
  48.         // for exclusive mode, we do need AllocPresenterDDXclMode of IVMRSurfaceAllocator
  49.         CHECK_HR(hr = CoCreateInstance(CLSID_AllocPresenterDDXclMode, NULL,
  50.                               CLSCTX_INPROC_SERVER,
  51.                               __uuidof(IVMRSurfaceAllocator),
  52.                               (LPVOID*)&m_lpDefSA));
  53.  
  54.         CHECK_HR(hr = m_lpDefSA->QueryInterface(__uuidof(IVMRImagePresenterExclModeConfig),
  55.                                                 (LPVOID*)&lpConfig));
  56.  
  57.         CHECK_HR(hr = lpConfig->SetRenderingPrefs(RenderPrefs_ForceOffscreen));
  58.         
  59.         // this sets exclusive mode
  60.         CHECK_HR(hr = lpConfig->SetXlcModeDDObjAndPrimarySurface(lpDD, lpPS));
  61.  
  62.         CHECK_HR(hr = m_lpDefSA->QueryInterface(__uuidof(IVMRImagePresenter),
  63.                                                 (LPVOID*)&m_lpDefIP));
  64.  
  65.         CHECK_HR(hr = m_lpDefSA->QueryInterface(__uuidof(IVMRWindowlessControl),
  66.                                                 (LPVOID*)&m_lpDefWC));
  67.  
  68.         CHECK_HR(hr = m_lpDefWC->SetVideoClippingWindow(m_hwndApp));
  69.         CHECK_HR(hr = m_lpDefSA->AdviseNotify(this));
  70.     }
  71.     __finally {
  72.  
  73.         RELEASE(lpConfig);
  74.  
  75.         if (FAILED(hr)) {
  76.             RELEASE(m_lpDefWC);
  77.             RELEASE(m_lpDefIP);
  78.             RELEASE(m_lpDefSA);
  79.         }
  80.     }
  81.  
  82.     return hr;
  83. }
  84.  
  85.  
  86. //----------------------------------------------------------------------------
  87. // NonDelegatingQueryInterface
  88. //
  89. //----------------------------------------------------------------------------
  90. STDMETHODIMP
  91. CMpegMovie::NonDelegatingQueryInterface(
  92.     REFIID riid,
  93.     void** ppv
  94.     )
  95. {
  96.     if (riid == __uuidof(IVMRSurfaceAllocator)) {
  97.         return GetInterface((IVMRSurfaceAllocator*)this, ppv);
  98.     }
  99.     else if (riid == __uuidof(IVMRImagePresenter)) {
  100.         return GetInterface((IVMRImagePresenter*)this, ppv);
  101.     }
  102.  
  103.     return CUnknown::NonDelegatingQueryInterface(riid,ppv);
  104. }
  105.  
  106. //////////////////////////////////////////////////////////////////////////////
  107. //
  108. // IVMRSurfaceAllocator-overriden functions
  109. //
  110. //////////////////////////////////////////////////////////////////////////////
  111.  
  112. //----------------------------------------------------------------------------
  113. // AllocateSurfaces
  114. //
  115. // call default AllocateSurface
  116. //----------------------------------------------------------------------------
  117. STDMETHODIMP
  118. CMpegMovie::AllocateSurface(
  119.     DWORD_PTR dwUserID,
  120.     VMRALLOCATIONINFO* lpAllocInfo,
  121.     DWORD* lpdwBuffer,
  122.     LPDIRECTDRAWSURFACE7* lplpSurface
  123.     )
  124. {
  125.     HRESULT hr = m_lpDefSA->AllocateSurface(dwUserID, lpAllocInfo,
  126.                                       lpdwBuffer, lplpSurface);
  127.     if( SUCCEEDED(hr))
  128.     {
  129.         m_lpSurf = *lplpSurface;
  130.     }
  131.     return hr;
  132. }
  133.  
  134.  
  135. //----------------------------------------------------------------------------
  136. // FreeSurfaces()
  137. //
  138. // Call default FreeSurface
  139. //----------------------------------------------------------------------------
  140. STDMETHODIMP
  141. CMpegMovie::FreeSurface(
  142.     DWORD_PTR dwUserID
  143.     )
  144. {
  145.     HRESULT hr = m_lpDefSA->FreeSurface(dwUserID);
  146.  
  147.     if( SUCCEEDED(hr))
  148.     {
  149.         m_lpSurf = NULL;
  150.     }
  151.     return hr;
  152. }
  153.  
  154.  
  155.  
  156. //----------------------------------------------------------------------------
  157. // PrepareSurface
  158. //
  159. // call default PrepareSurface
  160. //----------------------------------------------------------------------------
  161. STDMETHODIMP
  162. CMpegMovie::PrepareSurface(
  163.     DWORD_PTR dwUserID,
  164.     LPDIRECTDRAWSURFACE7 lplpSurface,
  165.     DWORD dwSurfaceFlags
  166.     )
  167. {
  168.     return m_lpDefSA->PrepareSurface(dwUserID, lplpSurface, dwSurfaceFlags);
  169. }
  170.  
  171.  
  172.  
  173. //----------------------------------------------------------------------------
  174. // AdviseNotify
  175. //
  176. // call default AdviseNotify
  177. //----------------------------------------------------------------------------
  178. STDMETHODIMP
  179. CMpegMovie::AdviseNotify(
  180.     IVMRSurfaceAllocatorNotify* lpIVMRSurfAllocNotify
  181.     )
  182. {
  183.     return m_lpDefSA->AdviseNotify(lpIVMRSurfAllocNotify);
  184. }
  185.  
  186.  
  187. //////////////////////////////////////////////////////////////////////////////
  188. //
  189. // IVMRSurfaceAllocatorNotify-overriden functions
  190. //
  191. //////////////////////////////////////////////////////////////////////////////
  192.  
  193. //----------------------------------------------------------------------------
  194. // AdviseSurfaceAllocator
  195. //
  196. // standard AdviseSurfaceAllocator
  197. //----------------------------------------------------------------------------
  198. STDMETHODIMP
  199. CMpegMovie::AdviseSurfaceAllocator(
  200.     DWORD_PTR dwUserID,
  201.     IVMRSurfaceAllocator* lpIVRMSurfaceAllocator
  202.     )
  203. {
  204.     return m_lpDefSAN->AdviseSurfaceAllocator(dwUserID, lpIVRMSurfaceAllocator);
  205. }
  206.  
  207.  
  208. //----------------------------------------------------------------------------
  209. // SetDDrawDevice
  210. //
  211. // standard SetDDrawDevice
  212. //----------------------------------------------------------------------------
  213. STDMETHODIMP
  214. CMpegMovie::SetDDrawDevice(LPDIRECTDRAW7 lpDDrawDevice,HMONITOR hMonitor)
  215. {
  216.     return m_lpDefSAN->SetDDrawDevice(lpDDrawDevice, hMonitor);
  217. }
  218.  
  219.  
  220. //----------------------------------------------------------------------------
  221. // ChangeDDrawDevice
  222. //
  223. // standard ChangeDDrawDevice
  224. //----------------------------------------------------------------------------
  225. STDMETHODIMP
  226. CMpegMovie::ChangeDDrawDevice(LPDIRECTDRAW7 lpDDrawDevice,HMONITOR hMonitor)
  227. {
  228.     return m_lpDefSAN->ChangeDDrawDevice(lpDDrawDevice, hMonitor);
  229. }
  230.  
  231.  
  232. //----------------------------------------------------------------------------
  233. // RestoreDDrawSurfaces
  234. //
  235. // standard RestoreDDrawSurfaces
  236. //----------------------------------------------------------------------------
  237. STDMETHODIMP CMpegMovie::RestoreDDrawSurfaces()
  238. {
  239.     // Make sure that the menu is redrawn
  240.     if( m_AlphaBlt )
  241.         m_AlphaBlt->SetMenuRestoreFlag();
  242.  
  243.     return m_lpDefSAN->RestoreDDrawSurfaces();
  244. }
  245.  
  246. //----------------------------------------------------------------------------
  247. // NotifyEvent
  248. //
  249. // standard NotifyEvent
  250. //----------------------------------------------------------------------------
  251. STDMETHODIMP
  252. CMpegMovie::NotifyEvent(LONG EventCode, LONG_PTR lp1, LONG_PTR lp2)
  253. {
  254.     return m_lpDefSAN->NotifyEvent(EventCode, lp1, lp2);
  255. }
  256.  
  257.  
  258. //----------------------------------------------------------------------------
  259. // SetBorderColor
  260. //
  261. // default SetBorderColor
  262. //----------------------------------------------------------------------------
  263. STDMETHODIMP
  264. CMpegMovie::SetBorderColor(
  265.     COLORREF clr
  266.     )
  267. {
  268.     return m_lpDefSAN->SetBorderColor(clr);
  269. }
  270.  
  271.  
  272. //////////////////////////////////////////////////////////////////////////////
  273. //
  274. // IVMRImagePresenter overriden functions
  275. // we perform all user customization here
  276. //
  277. //////////////////////////////////////////////////////////////////////////////
  278.  
  279. //----------------------------------------------------------------------------
  280. // StartPresenting()
  281. //
  282. //----------------------------------------------------------------------------
  283. STDMETHODIMP
  284. CMpegMovie::StartPresenting(DWORD_PTR dwUserID)
  285. {
  286.     return m_lpDefIP->StartPresenting(dwUserID);
  287. }
  288.  
  289.  
  290. //----------------------------------------------------------------------------
  291. // StopPresenting()
  292. //
  293. //----------------------------------------------------------------------------
  294. STDMETHODIMP
  295. CMpegMovie::StopPresenting(DWORD_PTR dwUserID)
  296. {
  297.     return m_lpDefIP->StopPresenting(dwUserID);
  298. }
  299.  
  300.  
  301. //----------------------------------------------------------------------------
  302. // PresentImage
  303. //
  304. // Here all the fun happens. lpPresInfo contains surface with current video image
  305. // Call m_AlphaBlt->AlphaBlt to perform all the necessary transformation
  306. //----------------------------------------------------------------------------
  307. STDMETHODIMP
  308. CMpegMovie::PresentImage(
  309.     DWORD_PTR dwUserID,
  310.     VMRPRESENTATIONINFO* lpPresInfo
  311.     )
  312. {
  313.     // clear the background
  314.     DDBLTFX ddFX;
  315.     INITDDSTRUCT(ddFX);
  316.  
  317.     RECT rcS = {0, 0, 640, 480};
  318.     RECT rcD = {128, 96, 512, 384};
  319.     RECT rcDt ={128, 0, 512, 288};
  320.  
  321.     m_lpDefWC->GetVideoPosition(&rcS, NULL);
  322.     if( g_ss.bShowTwist )
  323.     {
  324.         m_AlphaBlt->AlphaBlt(&rcDt, lpPresInfo->lpSurf, &rcS, 0xFF);
  325.     }
  326.     else
  327.     {
  328.         m_AlphaBlt->AlphaBlt(&rcD, lpPresInfo->lpSurf, &rcS, 0xFF);
  329.     }
  330.  
  331.     m_lpSurf->Flip(NULL,0);
  332.  
  333.     if( g_ss.bShowStatistics && m_Qp)
  334.     {
  335.         // call IQualProp functions here to get performance statistics
  336.         GetPerformance();
  337.     }
  338.  
  339.     // Show the scene
  340.     m_pDDObject.GetFB()->Flip(NULL, /*DDFLIP_WAIT*/ 0);
  341.  
  342.     return S_OK;
  343. }
  344.  
  345. //----------------------------------------------------------------------------
  346. //  GetPerformance
  347. //
  348. //  Calls IQualProp::get_AvgFrameRate   
  349. //  every 25 frames (to not overload VMR with senseless calculations)
  350. //  and saves this value to g_ss, global SceneSettings structure
  351. // 
  352. //----------------------------------------------------------------------------
  353. void 
  354. CMpegMovie::GetPerformance()
  355. {
  356.     
  357.     static int nCounter = 0;
  358.     static int nAvgFrameRate = 0;
  359.     char szFrameRate[MAX_PATH];
  360.     
  361.     nCounter++;
  362.     if( 25 == nCounter )
  363.     {
  364.         m_Qp->get_AvgFrameRate( &nAvgFrameRate);
  365.         nCounter = 0;
  366.     }
  367.     sprintf( szFrameRate, "FPS: %f  ", (float)nAvgFrameRate/100.f);
  368.     lstrcpy(g_ss.achFPS, TEXT(szFrameRate));
  369.     return;
  370. }
  371.  
  372.